//app.js
const baseUrl = require('./config').baseUrl
App({
    onLaunch (option) {
        let This = this
        wx.login({
            success (res) {
                if (res.code) {
                    //发起网络请求
                    wx.request({
                        url: baseUrl,
                        header: {
                            "content-type": "application/x-www-form-urlencoded"
                        },
                        data: {
                            data: JSON.stringify({
                                business: "GetOpenId",
                                data: res.code
                            })
                        },
                        method: 'POST',
                        success: (res) => {
                            if (res.data.code === 0) {
                                This.globalData.account = res.data.account
                                This.globalData.username = res.data.username
                                This.globalData.deptname = res.data.deptname
                                This.globalData.email = res.data.email
                                This.globalData.tel = res.data.tel
                                This.globalData.deptid = res.data.deptid
                                if (This.accountCallback) {
                                    This.accountCallback(res.data.account);
                                }
                            } else {
                                wx.showToast({
                                    title: res.data.info,
                                    icon: 'none',
                                    duration: 1000
                                })
                            }
                        },
                        fail: (error) => {
                            wx.showToast({
                                title: error.data.info,
                                icon: 'none',
                                duration: 1000
                            })
                        }
                    })
                } else {
                    console.log('登录失败！' + res.errMsg)
                }
            }
        })
        this.getcurrentPageUrl(option)
        if (!wx.getStorageSync('userInfo')) {
            wx.reLaunch({
                url: '/pages/authorize/index'
            })
        } else {
            this.globalData.userInfo = wx.getStorageSync('userInfo')
        }
    },
    globalData: {
        account: '', // account
        userInfo: null, // 用户信息
        username: '', // 名字
        deptname: '', // 单位名称
        email: '', // 邮箱
        tel: '', // 手机号
        deptid: '', // 用户的个人单位id
        currentPageUrl: '' // 当前页面url
    },
    getcurrentPageUrl (option) {
        let queryStr = ''
        let queryKeys = Object.keys(option.query)
        queryKeys.forEach((key, index) => {
            if (index === 0) {
                queryStr += `?${key}=${option.query[key]}`
            } else {
                queryStr += `&${key}=${option.query[key]}`
            }
        })
        this.globalData.currentPageUrl = `/${option.path}${queryStr}`
    },
    onGotUserInfo () {
        // 获取用户信息
        wx.getUserInfo({
            success: res => {
                // 可以将 res 发送给后台解码出 unionId       
                this.globalData.userInfo = res.userInfo
                wx.setStorageSync('userInfo', res.userInfo)
                wx.reLaunch({
                    url: this.globalData.currentPageUrl
                })
                // 所以此处加入 callback 以防止这种情况
                if (this.userInfoReadyCallback) {
                    this.userInfoReadyCallback(res)
                }
            }
        })
    }
})